package com.mk.Service.Impl;

import com.mk.Entity.Blog;
import com.mk.Entity.Comment;
import com.mk.Mapper.CommentMapper;
import com.mk.Service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {


    @Autowired
    CommentMapper commentMapper;

    @Override
    public List<Comment> queryAll(Blog blog) {
        //查询到一级评论
        List<Comment> comments = commentMapper.selectAll(blog.getId(), (long) -1);
        for (int i=0;i<comments.size();i++){
            Comment comment=comments.get(i);
            List<Comment> child = getAllchild(comment);
            comment.setChildComments(child);
            comments.set(i,comment);
        }

        return comments;
    }

    @Override
    public Comment queryOne(Long id) {
        Comment comment = commentMapper.selectOne(id);
        return comment;
    }

    @Override
    public void insertComment(Comment comment) {
        commentMapper.insert(comment);
    }

    @Override
    public void updateComment(Comment comment) {
        commentMapper.update(comment);
    }

    @Override
    public void deleteComment(Long id) {
        commentMapper.delete(id);
    }

    public List<Comment> getAllchild(Comment comment){
        Long pid = comment.getId();
        List<Comment> comments = commentMapper.selectAll(null, pid);
        for (int i=0;i<comments.size();i++){
            //将回复对象
            Comment comment1=new Comment();
            comment1=queryOne(comments.get(i).getReplyId());
            comments.get(i).setReplyComment(comment1);
        }
        return comments;
    }
}
